package com.base;

import javax.servlet.http.HttpServletRequest;

import com.utils.StringTool;

public class Utils {

	public static String htmlPageOpen() {
		String s = Constant.DOCTYPE_XHTML1_TRANSITIONAL + "<html "+Constant.HTML_XMLNS+">";
		s += "<head>"+Constant.META_CONTENT_TYPE_UTF8;
		return s;
	}
	
	public static String htmlCloseHeadOpenBody() {
		return "</head><body>";
	}
	
	public static String htmlPageClose() {
		return "</body></html>";
	}
	
	public static String redirect(String url) {
		return "<script>location.href='"+url+"';</script>";
	}
	
	public static String replaceHTMLChar(Object input) {
		if (input == null) return "";
		String s = (String) input;
		s = s.replaceAll("'", "&acute;");
		s = s.replaceAll("\"", "&quot;");
		s = s.replaceAll("<", "&lt;");
		s = s.replaceAll(">", "&gt;");
		return s;
	}
	
	public static String getParam(HttpServletRequest request, String name, String defaultValue) {
		String value = request.getParameter(name);
		if (StringTool.isEmptyOrNul(value)) value = defaultValue;
		return value;
	}
	
	public static String[] getParamValues(HttpServletRequest request, String name, String[] defaultValue) {
		String[] value = request.getParameterValues(name);
		if (value == null || value.length == 0) value = defaultValue;
		return value;
	}
	
	public static boolean checkRight(String parent, String sub) {
		boolean result = false;
		try {
			if ((","+parent+",").indexOf(","+sub+",")>=0) {
				result = true;
			}
		} catch (Exception e){}
		return result;
	}
	
}
